package com.oscar.ehome.provider.zxing.client.android.decode;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Browser;
import android.util.Log;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.oscar.ehome.R;
import com.oscar.ehome.provider.zxing.client.android.camera.CameraManager;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.view.loginview.CaptureActivity;

import java.util.Collection;

/**
 * This class handles all the messaging which comprises the state machine for capture.
 *
 * @author dswitkin@google.com (Daniel Switkin)
 */
public final class CaptureActivityHandler extends Handler {

    private static final String TAG = CaptureActivityHandler.class.getSimpleName();

    private final CaptureActivity activity;
    private final DecodeThread decodeThread;
    private State state;
    private final CameraManager cameraManager;
    private String scan_type;

    private enum State {
        PREVIEW, SUCCESS, DONE
    }

    public CaptureActivityHandler(CaptureActivity activity, ViewfinderView viewFinderView, Collection<BarcodeFormat> decodeFormats, String characterSet, CameraManager cameraManager
            , String scan_type) {
        this.activity = activity;
        this.scan_type = scan_type;
        decodeThread = new DecodeThread(activity, decodeFormats, characterSet, new ViewfinderResultPointCallback(viewFinderView), scan_type);
        decodeThread.start();
        state = State.SUCCESS;
        this.cameraManager = cameraManager;
        cameraManager.startPreview();
        restartPreviewAndDecode();
    }

    public CaptureActivityHandler(CaptureActivity activity, SweepViewfinderView viewFinderView, Collection<BarcodeFormat> decodeFormats, String characterSet, CameraManager cameraManager
            , String scan_type) {
        this.activity = activity;
        this.scan_type = scan_type;
        decodeThread = new DecodeThread(activity, decodeFormats, characterSet, new ViewfinderResultPointCallback(viewFinderView), scan_type);
        decodeThread.start();
        state = State.SUCCESS;
        this.cameraManager = cameraManager;
        cameraManager.startPreview();
        restartPreviewAndDecode();
    }

    @Override
    public void handleMessage(Message message) {
        int what = message.what;
        if (what == R.id.restart_preview) {
            restartPreviewAndDecode();
        } else if (what == R.id.decode_succeeded) {
            state = State.SUCCESS;
            Bundle bundle = message.getData();
            Bitmap barcode = bundle == null ? null : (Bitmap) bundle.getParcelable(DecodeThread.BARCODE_BITMAP);  //返回相机捕捉到图像
            if (CommonUtils.isIdcard == 1) {
                activity.handleDecodeTwo((String) message.obj, barcode); //返回身份证解析图片的信息
            } else {
                activity.handleDecode((Result) message.obj, barcode);//返回二维码图片的信息
            }
        } else if (what == R.id.decode_failed) {
            state = State.PREVIEW;
            cameraManager.requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
        } else if (what == R.id.return_scan_result) {
            activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
            activity.finish();
        } else if (what == R.id.launch_product_query) {
            Log.d(TAG, "Got product query message");
            String url = (String) message.obj;

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            intent.setData(Uri.parse(url));

            ResolveInfo resolveInfo = activity.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
            String browserPackageName = null;
            if (resolveInfo.activityInfo != null) {
                browserPackageName = resolveInfo.activityInfo.packageName;
                Log.d(TAG, "Using browser in package " + browserPackageName);
            }

            // Needed for default Android browser only apparently
            if ("com.android.browser".equals(browserPackageName)) {
                intent.setPackage(browserPackageName);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Browser.EXTRA_APPLICATION_ID, browserPackageName);
            }

            try {
                activity.startActivity(intent);
            } catch (ActivityNotFoundException anfe) {
                Log.w(TAG, "Can't find anything to handle VIEW of URI " + url);
            }
        }
    }

    public void quitSynchronously() {
        state = State.DONE;
        cameraManager.stopPreview();
        Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
        try {
//            quit.sendToTarget();
            // Wait at most half a／ second; should be enough time, and onPause() will timeout quickly
            decodeThread.join(500L);
        } catch (InterruptedException e) {
            // continue
        } catch (RuntimeException e) {
            removeMessages(R.id.decode_succeeded);
            removeMessages(R.id.decode_failed);
        }
//        09-11 11:32:18.188 7398-7398/com.oscar.ehome W/MessageQueue: Handler (com.oscar.ehome.provider.zxing.client.android.decode.DecodeHandler) {42455298} sending message to a Handler on a dead thread

        // Be absolutely sure we don't send any queued up messages
        removeMessages(R.id.decode_succeeded);
        removeMessages(R.id.decode_failed);
    }

    private void restartPreviewAndDecode() {
        if (state == State.SUCCESS) {
            state = State.PREVIEW;
            cameraManager.requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
            activity.drawViewfinder();
        }
    }

}
